import store from "@/store";

const state = {
    token:'',
    themeInfo:{},
    chatHistoryMessage:[],
    sendMessages:[],
    apiKey:'',
    baseUrl:'https://dashscope.aliyuncs.com/compatible-mode/v1',
    aliModels:[
        'qwen-plus','qwen-plus-latest',
        'qwen2.5-coder-32b-instruct','qwen2.5-coder-14b-instruct','qwen2.5-coder-7b-instruct','qwen2.5-coder-3b-instruct','qwen2.5-coder-1.5b-instruct','qwen2.5-coder-0.5b-instruct','qwen-coder-plus','qwen-coder-turbo',
        'deepseek-r1','deepseek-r1-0528','deepseek-v3','deepseek-r1-distill-qwen-1.5b','deepseek-r1-distill-qwen-7b','deepseek-r1-distill-qwen-14b','deepseek-r1-distill-qwen-32b','deepseek-r1-distill-llama-8b',
        'deepseek-r1-distill-llama-70b'
    ],
    deepSeekModels:['deepseek-chat','deepseek-reasoner'],
    baiduModels:['ernie-4.5-turbo-128k','ernie-4.5-turbo-128k-preview','ernie-4.5-turbo-32k','ernie-4.5-8k-preview','ernie-4.0-8k','ernie-3.5-128k','ernie-speed-128k','ernie-speed-8k','ernie-tiny-8k'],
    hunyuanModels:['hunyuan-t1-latest','hunyuan-a13b','hunyuan-large','hunyuan-standard','hunyuan-lite'],
    activeModels:[],
    activeModel:'',
    requestStatus:false,
    isMessageDone:true,
    nowMessage:'',
    nowReasoningMessage:'',
    stopCommand:false,
    requestErrCount:0,
    projectInfo:{}
}

const mutations = {
    setToken(state,token) {
        state.token = token;
    },
    setThemeInfo(state,themeInfo) {
        let root = document.documentElement;
        root.style.setProperty('--background', themeInfo.background);
        root.style.setProperty('--fontColor', themeInfo.fontColor);
        root.style.setProperty('--liHoverBackground', themeInfo.liHoverBackground);
        root.style.setProperty('--inputBgColor', themeInfo.inputBgColor);
        root.style.setProperty('--inputLineColor', themeInfo.inputLineColor);
        root.style.setProperty('--lineColor', themeInfo.lineColor);
        root.style.setProperty('--scrollbarColor', themeInfo.scrollbarColor);


        let baseConfig = localStorage.getItem('baseConfig')
        baseConfig?baseConfig=JSON.parse(baseConfig):baseConfig={}
        baseConfig.themeInfo = themeInfo
        localStorage.setItem('baseConfig',JSON.stringify(baseConfig))

        state.themeInfo = themeInfo;
    },
    setChatHistoryMessage(state,chatHistoryMessage){
        state.chatHistoryMessage.push(chatHistoryMessage)
    },
    setSendMessages(state,sendMessages){
        if(state.sendMessages.length > 4){
            state.sendMessages.shift()
        }
        state.sendMessages.push(sendMessages)
    },
    setSendMessageErr(state,sendMessageErr){
        state.sendMessages.pop()
      let lastMessage = state.chatHistoryMessage.pop()
        lastMessage['status'] = 2
        state.chatHistoryMessage.push(lastMessage)
    },
    setBaseUrl(state,baseUrl){
        let baseConfig = localStorage.getItem('baseConfig')
        baseConfig?baseConfig=JSON.parse(baseConfig):baseConfig={}
        baseConfig.baseUrl = baseUrl
        localStorage.setItem('baseConfig',JSON.stringify(baseConfig))
        if(baseUrl&&baseUrl.indexOf('dashscope.aliyuncs.com')>-1){
            state.activeModels = state.aliModels
            // state.activeModel = state.aliModels[0]
        }else if(baseUrl&&baseUrl.indexOf('api.deepseek.com')>-1){
            state.activeModels = state.deepSeekModels
            // state.activeModel = state.deepSeekModels[0]
        }else if(baseUrl&&baseUrl.indexOf('hunyuan.cloud.tencent.com')>-1){
            state.activeModels = state.hunyuanModels
            // state.activeModel = state.hunyuanModels[0]
        }else if(baseUrl&&baseUrl.indexOf('qianfan.baidubce.com')>-1){
            state.activeModels = state.baiduModels
            // state.activeModel = state.baiduModels[0]
        }
        state.baseUrl = baseUrl
    },
    setActiveModel(state,activeModel){
        let baseConfig = localStorage.getItem('baseConfig')
        baseConfig?baseConfig=JSON.parse(baseConfig):baseConfig={}
        baseConfig.modelName = activeModel
        localStorage.setItem('baseConfig',JSON.stringify(baseConfig))
        console.log('修改模型')
        state.activeModel = activeModel
    },
    setRequestStatus(state,requestStatus){
        state.requestStatus = requestStatus
    },
    setApiKey(state,apiKey){
        let baseConfig = localStorage.getItem('baseConfig')
        baseConfig?baseConfig=JSON.parse(baseConfig):baseConfig={}
        baseConfig.apiKey = apiKey
        state.apiKey = apiKey
    },
    setIsMessageDone(state,isMessageDone){
        state.isMessageDone = isMessageDone
    },
    setNowMessage(state,nowMessage){
        state.nowMessage = nowMessage
    },
    setActiveModels(state,activeModels){
        state.activeModels = activeModels
    },
    setRequestErrCount(state,requestErrCount){
        state.requestErrCount = requestErrCount
    },
    setProjectInfo(state,projectInfo){
        state.projectInfo = projectInfo
    },
    setNowReasoningMessage(state,nowReasoningMessage){
        state.nowReasoningMessage = nowReasoningMessage
    }
}

const actions = {
    reSendMessage(context,message){
        let lastMessage = state.chatHistoryMessage.pop()
        lastMessage['status'] = 1
        state.chatHistoryMessage.push(lastMessage)
    },
    stopMsg(context,status){
        state.stopCommand = status
        if (status){
            state.isMessageDone = true
            state.requestStatus = false
        }
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}